/*
 * allocations.h
 *
 * Created: 6/13/2013 4:26:51 AM
 *  Author: Tim
 */ 


#ifndef ALLOCATIONS_H_
#define ALLOCATIONS_H_

#include <avr/io.h>

enum ioSrc
	{
		DIO_FREE		=	0,
		DIO_INPUT		=	1,
		DIO_OUTPUT		=	2,
		
		DIO_ENC_CHA		=	3,
		DIO_ENC_CHB		=	4,
		DIO_ENC_INDX	=	5,
		
		DIO_PWM			=	6,
		
		DIO_ICP			=	7
		
		
};

enum Alloc
{
	FEATURE_FREE		= 0,
	FEATURE_UTILIZED	= 1
};

typedef enum TIMER_ALLOC
{
	TIMER_NULL			= 0,
	TIMER_C0			= 1,
	TIMER_C1			= 2,
	TIMER_D0			= 3,
	TIMER_D1			= 4,
	TIMER_E0			= 5
}tmrAlloc_t;

typedef enum AQUISITION_STATE
{
	AQ_UKNOWN_STATE		=0,
	AQ_INIT_STATE		=1,
	AQ_AQUIRE_STATE		=2,
	AQ_SEND_STATE		=3,
	AQ_END_STATE		=4,
	
}daqAqState_t;

typedef struct DAQ_ALLOCATIONS
{	
	struct timerAllocation
	{
		uint8_t timerC0Alloc	:1;
		uint8_t timerC1Alloc	:1;
		uint8_t timerD0Alloc	:1;
		uint8_t timerD1Alloc	:1;
		uint8_t timerE0Alloc	:1;
		uint8_t reserved		:3;
	}timerAlloc;
	
	//a '1' signifies that the analog channel is being used
	struct analogAllocation
	{
		uint8_t analogInCh0Alloc	:1;
		uint8_t analogInCh1Alloc	:1;
		uint8_t analogInCh2Alloc	:1;
		uint8_t analogInCh3Alloc	:1;
		uint8_t analogOutCh0Alloc	:1;
		uint8_t analogOutCh1Alloc	:1;
		uint8_t reserved			:2;
	}analogAlloc;	
	
	//encXalloc signifies if the specific encoder is in use
	//encXTmrAlloc signifies what timer counter the encoder
	//is utilizing for QDEC 
	struct encoderAllocations
	{
		uint8_t enc0Alloc		:1;
		uint8_t enc0Abs			:1;
		uint8_t enc0Index		:1;
		uint8_t enc1Alloc		:1;
		uint8_t enc1Abs			:1;
		uint8_t enc1Index		:1;
		uint8_t	reserved		:2;
	}encAlloc;
	
	struct actuatorAllocations
	{
		uint8_t motor0Alloc		:1;
		uint8_t motor1Alloc		:1;
		uint8_t motor2Alloc		:1;
		uint8_t motor3Alloc		:1;
		
		uint8_t servo0Alloc		:1;
		uint8_t servo1Alloc		:1;
		uint8_t servo2Alloc		:1;
		uint8_t servo3Alloc		:1;
		
	}actuatorAlloc;
	
	
	volatile uint8_t digitalOut;
	volatile uint8_t digitalIn;
	
	
	struct dioALLOC{
		uint8_t alloc		:1; //feature free/utilized
		uint8_t inversion	:1;
		uint8_t source		:6; //type ioSrc 
		
	}dio[13];
	
	
}daqAlloc_t;					


typedef struct CalibrationValues
{
	int16_t ADC_OFFSET_CAL_2X[4];
	int16_t ADC_OFFSET_CAL_4X[4];
	int16_t ADC_OFFSET_CAL_8X[4];
	int16_t ADC_OFFSET_CAL_16X[4];
	int16_t ADC_OFFSET_CAL_32X[4];
	int16_t ADC_OFFSET_CAL_64X[4];

	int16_t ADC_CH_OFFSET[4];	//offset for the channel
	
}daqCal_t;

daqAlloc_t daqAlloc;
daqCal_t daqCal;

void allocTimer(tmrAlloc_t tmr);
void unAllocTimer(tmrAlloc_t tmr);


volatile uint8_t	outputSuperPacketLength;
volatile uint8_t	inputSuperPacketLength;
volatile uint8_t	actuatorSuperPacketLength;

#endif /* ALLOCATIONS_H_ */